package org.manage.web;

import org.manage.dto.PageBean;
import org.manage.entity.Department;
import org.manage.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2018/9/6.
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(@RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage, Integer pageSize, Model model) {
        PageBean<Department> pageBean = departmentService.getDepartmentByPage(currentPage, pageSize);
        model.addAttribute("pageBean", pageBean);
        return "/frame/list";
    }

    @RequestMapping(value = "/{did}/update", method = RequestMethod.GET)
    public String edit(@PathVariable("did") Integer did, Model model) {
        Department department = departmentService.getDepartmentById(did);
        model.addAttribute("d", department);
        return "/frame/editDepartment";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@ModelAttribute Department department) {
        departmentService.updateDepartment(department);
        return "redirect:/department/list";
    }

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public String addDepartment() {
        return "/frame/addDepartment";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveDepartment(Department department) {
        departmentService.saveDepartment(department);
        return "redirect:/department/list";
    }

    @RequestMapping(value = "/{did}/delete", method = RequestMethod.GET)
    public String deleteDepartment(@PathVariable("did") Integer did) {
        departmentService.deleteDepartment(did);
        return "redirect:/department/list";
    }

}
